/**
 *  列表页
 *
 * 作者 zhangwei
 * 日期 2024-09-02 11:25:48
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-hazardExamineTeam'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#hazardExamineTeam-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'hazardExamineTeamTable',
            url: ctx + 'hazardExamineTeam/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'examineTeamId', title: '主键',hide:true},
                {field: 'tenantId', title: '公司Id',hide:true},
                {field: 'teamName', title: '小组名称', minWidth: 180},
                {field: 'teamUserList', title: '小组成员', minWidth: 500, templet: function (d) {
                        var flg = '';
                        var users = d.teamUserList;
                        if(users){
                            users.forEach((item, index)=> {
                                flg += '<span class="layui-badge febs-bg-orange" style="margin-left:5px; ">'+item.realName+'</span>'
                            })
                        }
                        return flg;
                    }
                },
                {field: 'remark', title: '备注', minWidth: 120},
                {field: 'sort', title: '排序', width: 80,align:'center'},
                {field: 'status', title: '状态',toolbar: '#status-switch',width: 120,align:'center'},
                {field: '', toolbar: '#hazardExamineTeam-tool',title: '操作', width: 160,align:'center'}
            ]],
            toolbar: '#hazardExamineTeam-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(hazardExamineTeamTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    form.on('switch(status)', function (obj) {
        //状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其id
        var id =  table.cache['hazardExamineTeamTable'][index].examineTeamId;
        febs.modal.confirm('更改小组状态', '确认更改小组状态',function (){
            let data={
                examineTeamId:id,
                status:status,
                editStatus:true,
            }
            febs.postArray(ctx + 'hazardExamineTeam/update', data, function () {
                layer.closeAll();
                febs.alert.success('更新成功');
                $('#febs-hazardExamineTeam').find('#query').trigger('click');
            });
        },function (){
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });

    table.on('toolbar(hazardExamineTeamTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增', 'hazardExamineTeam/add', {
                btn: ['保存', '取消'],
                area: $(window).width() <= 750 ? ['75%', '75%']: ['50%', '55%'],
                yes: function (index, layero) {
                    $('#hazardExamineTeam-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId: currentTenantId,
                }
            });
        }
    });

    table.on('tool(hazardExamineTeamTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;
        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.examineTeamId);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('修改', 'hazardExamineTeam/update/' + data.examineTeamId, {
                area: $(window).width() <= 750 ? ['75%', '75%']: ['50%', '55%'],
                btn: ['保存', '取消'],
                yes: function (index, layero) {
                    $('#hazardExamineTeam-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {
                    tenantId:data.tenantId,
                    examineTeamId:data.examineTeamId
                },
            });
        }
    });


    function getQueryParams() {
        return {
            tenantId: $searchForm.find('select[name="tenantId"]').find('option:selected').val(),
            teamName: $searchForm.find('input[name="teamName"]').val()?.trim(),
            realName: $searchForm.find('input[name="realName"]').val()?.trim(),
            status:$searchForm.find('select[name="status"]').find('option:selected').val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'hazardExamineTeam/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});